/**
 * 
 */
package nus.mtech.presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import nus.mtech.ApplicationFacade;
import nus.mtech.constant.PresentationConstant;
import nus.mtech.customer.Customer;
import nus.mtech.customer.CustomerManager;
import nus.mtech.subscription.*;
import nus.mtech.subscription.ExtraFeature.ExtraFeatureType;
import nus.mtech.util.*;

import javax.swing.JComboBox;

/**
 * @author Zhang Zhong
 * 
 * 
 */
final class DigitalPanel extends JPanel implements ActionListener {

	private Dimension screen = null;
	private static volatile DigitalPanel INSTANCE;
	private JButton btnClear;
	private JButton btnUpdate;
	private JButton btnSearch;
	private JButton btnTransfer;
	private JComboBox comboBoxStatus;
	private JComboBox comboBoxOptionsStatus;
	private JComboBox comboBoxOptions;
	private JTextField txtAccountNo;
	private JTextField txtStartDate;
	private JTextField txtEndDate;
	private JTextField txtOptionStartDate;
	private JTextField txtOptionEndDate;
	private JTextField txtBoxTelNo;
	private ExtraFeature extraFeature;

	private SubscriptionManager subsManager = SubscriptionManager
			.getSubscriptionManagerInstance();

	DigitalPanel() {
		super(null);
		this.setBackground(PresentationConstant.CONSOLE_BACKGROUND);
		this.setPreferredSize(new Dimension(1004, 643));
		initialization();

	}

	public void initialization() {
		try {
			removeAll();

			screen = Toolkit.getDefaultToolkit().getScreenSize();

			final JLabel digitalPanelLabel = new JLabel();
			digitalPanelLabel.setHorizontalAlignment(SwingConstants.CENTER);
			digitalPanelLabel.setFont(new Font("", Font.BOLD, 16));
			digitalPanelLabel.setText("Digital Voice");
			digitalPanelLabel.setBounds(0, 10, 1004, 21);
			add(digitalPanelLabel);

			JLabel lblAccountNo = new JLabel("Account No:");
			lblAccountNo.setBounds(90, 133, 106, 14);
			add(lblAccountNo);

			btnClear = new JButton("Clear");
			btnClear.setBounds(254, 316, 89, 23);
			btnClear.addActionListener(this);
			add(btnClear);

			JLabel lblDigitalVoice = new JLabel("Digital Voice Subscriptions");
			lblDigitalVoice.setFont(new Font("Dialog", Font.BOLD, 14));
			lblDigitalVoice.setBounds(90, 88, 242, 21);
			add(lblDigitalVoice);

			JLabel lbTelephoneNo = new JLabel("Digital Voice No:");
			lbTelephoneNo.setBounds(90, 170, 106, 14);
			add(lbTelephoneNo);

			JLabel lblStartDate = new JLabel("Date Commenced:");
			lblStartDate.setBounds(90, 201, 106, 14);
			add(lblStartDate);

			JLabel lblStatus = new JLabel("Status:");
			lblStatus.setBounds(90, 270, 106, 14);
			add(lblStatus);

			comboBoxStatus = new JComboBox();
			comboBoxStatus.setBounds(206, 267, 137, 20);
			comboBoxStatus.addItem("");
			comboBoxStatus.addItem("Active");
			comboBoxStatus.addItem("Terminate");
			add(comboBoxStatus);

			btnUpdate = new JButton("Update / Add");
			btnUpdate.setBounds(125, 316, 106, 23);
			btnUpdate.addActionListener(this);
			add(btnUpdate);

			btnSearch = new JButton("Search");
			btnSearch.setBounds(502, 129, 89, 23);
			btnSearch.addActionListener(this);
			add(btnSearch);

			txtAccountNo = new JTextField();
			txtAccountNo.setColumns(10);
			txtAccountNo.setBounds(206, 130, 248, 20);
			add(txtAccountNo);

			txtBoxTelNo = new IntegerTextField();
			txtBoxTelNo.setEditable(true);
			txtBoxTelNo.setBounds(206, 167, 248, 20);
			add(txtBoxTelNo);

			txtStartDate = new JTextField();
			txtStartDate.setColumns(10);
			txtStartDate.setBounds(206, 198, 248, 20);
			add(txtStartDate);

			JLabel lblEndDate = new JLabel("Date Terminated:");
			lblEndDate.setBounds(90, 235, 106, 14);
			add(lblEndDate);

			txtEndDate = new JTextField();
			txtEndDate.setColumns(10);
			txtEndDate.setBounds(206, 232, 248, 20);
			add(txtEndDate);

			comboBoxOptionsStatus = new JComboBox();
			comboBoxOptionsStatus.setEnabled(false);
			comboBoxOptionsStatus.setBounds(206, 463, 137, 20);
			comboBoxOptionsStatus.addItem("Not Registered");
			comboBoxOptionsStatus.addItem("Active");
			comboBoxOptionsStatus.addItem("Terminate");
			add(comboBoxOptionsStatus);

			JLabel lblOptions = new JLabel("Options:");
			lblOptions.setBounds(90, 430, 106, 14);
			add(lblOptions);

			JLabel lblExtraOptions = new JLabel("Extra Options");
			lblExtraOptions.setFont(new Font("Dialog", Font.BOLD, 14));
			lblExtraOptions.setBounds(90, 398, 242, 21);
			add(lblExtraOptions);

			JLabel lblStartDate2 = new JLabel("Date Commenced:");
			lblStartDate2.setBounds(90, 501, 106, 14);
			add(lblStartDate2);

			JLabel lblEndDate2 = new JLabel("Date Terminated:");
			lblEndDate2.setBounds(90, 536, 106, 14);
			add(lblEndDate2);

			txtOptionStartDate = new JTextField();
			txtOptionStartDate.setEnabled(false);
			txtOptionStartDate.setColumns(10);
			txtOptionStartDate.setBounds(206, 498, 248, 20);
			add(txtOptionStartDate);

			txtOptionEndDate = new JTextField();
			txtOptionEndDate.setEnabled(false);
			txtOptionEndDate.setColumns(10);
			txtOptionEndDate.setBounds(206, 533, 248, 20);
			add(txtOptionEndDate);

			btnTransfer = new JButton("Update");
			btnTransfer.setEnabled(false);
			btnTransfer.setBounds(125, 572, 106, 23);
			btnTransfer.addActionListener(this);
			add(btnTransfer);

			JLabel lblOptionStatus = new JLabel("Status:");
			lblOptionStatus.setBounds(90, 466, 106, 14);
			add(lblOptionStatus);

			comboBoxOptions = new JComboBox();
			comboBoxOptions.setEnabled(false);
			comboBoxOptions.setBounds(206, 427, 137, 20);
			comboBoxOptions.addItem("Please Select");
			comboBoxOptions.addItem("Call Transfer");
			add(comboBoxOptions);

			JLabel lblCallTransferBorder = new JLabel();
			lblCallTransferBorder.setBounds(40, 381, 944, 227);
			lblCallTransferBorder.setBorder(new EtchedBorder(
					EtchedBorder.LOWERED));
			add(lblCallTransferBorder);

			JLabel lblBorder = new JLabel();
			lblBorder.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
			lblBorder.setBounds(40, 71, 944, 290);
			add(lblBorder);

		} catch (Exception ex) {
			System.out.println("exception");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	private void enableEditMode(boolean b) {
		btnSearch.setEnabled(!b);
		txtAccountNo.setEditable(!b);
		comboBoxOptions.setEnabled(b);
		comboBoxOptionsStatus.setEnabled(b);
		txtOptionStartDate.setEnabled(b);
		txtOptionEndDate.setEnabled(b);
		btnTransfer.setEnabled(b);
		btnUpdate.setEnabled(b);
	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == btnClear) {
			clear();
		} else if (ae.getSource() == btnSearch) {
			if (txtAccountNo.getText().length() == 0) {
				JOptionPane.showMessageDialog(this, "Account No. Required.",
						"Please enter Account No", JOptionPane.ERROR_MESSAGE);
			} else {
				Customer cust = ApplicationFacade.getInstance()
						.getCustomerByAccountNo(txtAccountNo.getText().trim());
				if (cust == null) {
					JOptionPane.showMessageDialog(this,
							"Account No. Not Found.",
							"Please re-enter Account No",
							JOptionPane.ERROR_MESSAGE);
				} else {
					txtAccountNo.setText(txtAccountNo.getText().trim()
							.toUpperCase());
					enableEditMode(true);
					DigitalVoice dv = getDigitalVoice(txtAccountNo.getText());
					if (dv != null) {
						txtBoxTelNo.setText(Long.toString(dv.getTelephoneNo()));
						txtStartDate.setText(DateUtil.formatDate(dv
								.getStartDate()));
						txtEndDate
								.setText(DateUtil.formatDate(dv.getEndDate()));
						if (dv.getStatus() == true)
							comboBoxStatus.setSelectedIndex(1);
						else
							comboBoxStatus.setSelectedIndex(2);
						ExtraFeature ef = dv.getExtraFeature();
						if (ef != null) {
							this.extraFeature = ef;
							comboBoxOptions.setSelectedIndex(1);
							comboBoxOptionsStatus.setSelectedIndex(1);
							txtOptionStartDate.setText(DateUtil.formatDate(ef
									.getStartDate()));
							if (ef.getEndDate() != null) {
								txtOptionEndDate.setText(DateUtil.formatDate(ef
										.getEndDate()));
								comboBoxOptionsStatus.setSelectedIndex(2);
							}
						} else {
							comboBoxOptionsStatus.setSelectedIndex(0);
							comboBoxOptions.setSelectedIndex(0);
							txtOptionStartDate.setText("");
							txtOptionEndDate.setText("");
						}
					} else {
						JOptionPane.showMessageDialog(this,
								"No existing Digital Voice Subscription",
								"No subscription", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		} else if (ae.getSource() == btnUpdate) {
			if (txtBoxTelNo.getText().length() == 0)
				JOptionPane.showMessageDialog(this, "Telephone No. Required.",
						"Please enter Telephone No", JOptionPane.ERROR_MESSAGE);
			else {
				try {
					long telno = Long.parseLong(txtBoxTelNo.getText().trim());
					Date startDate = Calendar.getInstance().getTime();
					if (txtStartDate.getText().length() > 0)
						startDate = DateUtil.parseDate(txtStartDate.getText()
								.trim(), DateUtil.DATE_FORMAT);
					Date endDate = null;
					if (txtEndDate.getText().trim().length() > 0)
						endDate = DateUtil.parseDate(txtEndDate.getText()
								.trim(), DateUtil.DATE_FORMAT);
					boolean status = true;
					if (comboBoxStatus.getSelectedIndex() == 2) {
						status = false;
						if (endDate == null) {
							endDate = Calendar.getInstance().getTime();
							txtEndDate.setText(DateUtil.formatDate(endDate));
						}
					}
					DigitalVoice dv = new DigitalVoice(telno, startDate,
							endDate, status);
					dv.setExtraFeature(this.extraFeature);
					subsManager.addSubscription(txtAccountNo.getText(), dv);
					JOptionPane
							.showMessageDialog(
									this,
									"Add/Update Digital Voice subscription successfully.",
									"Done", JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(this, ex.getMessage(),
							"Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		} else if (ae.getSource() == btnTransfer) {
			if (comboBoxOptions.getSelectedIndex() == 0)
				JOptionPane
						.showMessageDialog(this, "Extra option required.",
								"Please select extra option",
								JOptionPane.ERROR_MESSAGE);
			else {
				DigitalVoice dv = getDigitalVoice(txtAccountNo.getText());
				if (dv == null)
					JOptionPane.showMessageDialog(this,
							"Extra option required.",
							"Please select extra option",
							JOptionPane.ERROR_MESSAGE);
				else {
					boolean status = false;
					Date startDate = Calendar.getInstance().getTime();
					if (txtOptionStartDate.getText().trim().length() > 0) {
						startDate = DateUtil.parseDate(txtOptionStartDate
								.getText().trim(), DateUtil.DATE_FORMAT);
					} else
						txtOptionStartDate.setText(DateUtil.formatDate(
								startDate, DateUtil.DATE_FORMAT));

					Date endDate = null;
					if (comboBoxOptionsStatus.getSelectedIndex() == 0)
						comboBoxOptionsStatus.setSelectedIndex(1);
					if (comboBoxOptionsStatus.getSelectedIndex() == 1)
						status = true;
					else if (comboBoxOptionsStatus.getSelectedIndex() == 2) {
						status = false;
						if (endDate == null) {
							endDate = Calendar.getInstance().getTime();
							txtOptionEndDate.setText(DateUtil
									.formatDate(endDate));
							dv.setEndDate(endDate);
						}
					}
					this.extraFeature = new ExtraFeature(
							ExtraFeatureType.CallTransfer, startDate, endDate,
							status);
					subsManager.addExtraFeature(ExtraFeatureType.CallTransfer,
							startDate, endDate, status, dv);
					JOptionPane.showMessageDialog(this,
							"Update Digital Voice extra option successfully.",
							"Done", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}

	private DigitalVoice getDigitalVoice(String accountno) {
		ArrayList<Subscription> subses = ApplicationFacade.getInstance()
				.getSubscription(txtAccountNo.getText().trim());
		DigitalVoice dv = null;
		if (subses != null) {
			for (Subscription s : subses) {
				if (s instanceof DigitalVoice) {
					dv = (DigitalVoice) s;
					break;
				}
			}
		}
		return dv;
	}

	public void clear() {
		btnSearch.setEnabled(true);
		txtAccountNo.setEditable(true);
		enableEditMode(false);
		comboBoxStatus.setSelectedIndex(0);
		comboBoxOptionsStatus.setSelectedIndex(0);
		comboBoxOptions.setSelectedIndex(0);
		txtAccountNo.setText("");
		txtBoxTelNo.setText("");
		txtStartDate.setText("");
		txtEndDate.setText("");
		txtOptionStartDate.setText("");
		txtOptionEndDate.setText("");
	}

	public static DigitalPanel getInstance() {
		synchronized (DigitalPanel.class) {
			if (INSTANCE == null) {
				INSTANCE = new DigitalPanel();
			}
			return INSTANCE;
		}
	}

	public void doUpdate() {
		clear();
		// Update the Date/Time Combo Box
	}
}
